package com.frame.work.frame.deal.device0fbe;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.frame.work.common.constant.TypeEnum;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.frame.deal.util.DeviceRelationMessageUtil;
import com.frame.work.frame.deal.util.WeakCurrentDeviceUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.*;


/**
 * 水浸传感器
 */
@Data
public class ShuiJinChuanGanQi_0FBE0502 extends BaseFrame {

    private String switchState;

    public ShuiJinChuanGanQi_0FBE0502(String gatewayMac, String data) {

        //将基础数据进行处理
        super(gatewayMac, data);
        //核心业务处理
        CoreLogicDeal(data);
        //配置Action信息
        getActions(data);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, JSONObject.toJSONString(this));
    }


    //**************************************************************************************************************

    /**
     * @Description:获取报警状态
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/1/27 10:33
     */
    private String getAlarmState(String data) {
        String alarmState = StringUtils.substring(data, data.length() - 8, data.length() - 6);
        this.setSwitchState(alarmState);
        return alarmState;
    }

    /**
     * @Description:获取防拆状态
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/1/27 10:34
     */
    private static String getFangChaiState(String data) {
        return StringUtils.substring(data, data.length() - 6, data.length() - 4);
    }

    /**
     * @Description:获取电压
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/1/27 10:40
     */
    private static String getDianya(String data) {
        String dianya = StringUtils.substring(data, data.length() - 16, data.length() - 12);
        dianya = dianya.substring(2, 4) + dianya.substring(0, 2);
        dianya = dianya.substring(0, 1) + "." + dianya.substring(1, 4);
        return dianya;
    }

    /**
     * @Description:设置Action信息
     * @param: data
     * @return: void
     * @author:wjming
     * @Date:2021/1/27 19:20
     */
    private void getActions(String data) {

        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("reportType", this.getDataType());
        actions.put("alarmState", getAlarmState(data));
        actions.put("dianya", getDianya(data));
        actions.put("fangChaiState", getFangChaiState(data));
        this.setActions(actions);
    }

    /**
     * @Description:核心逻辑处理
     * @param: data
     * @return: void
     * @author:wjming
     * @Date:2021/1/27 20:38
     */
    public void CoreLogicDeal(String data) {
        String type = "";
        String content = "";
        Boolean isSaveAndSend = false;
        switch (this.getDataType()) {
            case "00"://触发上报
                //报警
                type = StringUtils.equals("01", getAlarmState(data)) ? TypeEnum.WARNING.code() : TypeEnum.STATE.code();
                content = StringUtils.equals("01", getAlarmState(data)) ? "警报" : "解除警报";
                isSaveAndSend = true;
                break;
            case "80"://主动定时上报(只发送mqtt)
                //查询设备action信息
                List<String> list = new LinkedList<>();
                list.add("alarmState");
                list.add("fangChaiState");
                List<String> deviceActionMessage = DeviceRelationMessageUtil.getDeviceActionParamsValue(this.getDeviceMac(), this.getOdIndex(),
                        this.getDeviceType(), this.getProductType(), list);
                if (CollectionUtils.isEmpty(deviceActionMessage)) {
                    return;
                }
                //防拆开关松动
                if (!StringUtils.equals(getFangChaiState(data), deviceActionMessage.get(1))) {
                    type = StringUtils.equals("01", getFangChaiState(data)) ? TypeEnum.NORMAL.code() : TypeEnum.STATE.code();
                    content = StringUtils.equals("01", getFangChaiState(data)) ? "松动" : "恢复正常";
                    isSaveAndSend = true;
                }
                break;
        }
        if (isSaveAndSend) {
            WeakCurrentDeviceUtil.SaveLogAndSendJpush(this.getDeviceMac(), this.getGatewayMac(), content, type);
        }
    }

}
